package com.wangzaiplus.test.designpattern.yuanxing;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @ClassName Shape
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/17 10:27
 * @Version V1.0
 **/
public interface  Shape extends  Cloneable {
    public   Object   clone();//拷贝
    public void countArea();//计算面积
}
class  Circle implements  Shape{
    @Override
    public Object clone() {
        Circle c=null;
        try {
            c=(Circle)super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("拷贝圆失败!");
        }
        return c;
    }

    @Override
    public void countArea() {
        int r=0;
        System.out.print("这是一个圆，请输入圆的半径：");
        Scanner scanner=new Scanner(System.in);
        r= scanner.nextInt();
        System.out.println("该圆的面积="+3.1415*r*r+"\n");
    }
}

class  Square   implements  Shape {
    @Override
    public Object clone() {
        Square square=null;
        try {
            square=(Square)super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("拷贝正方形失败!");
        }
        return square;
    }

    @Override
    public void countArea() {
        int a=0;
        System.out.print("这是一个正方形，请输入它的边长：");
        Scanner input=new Scanner(System.in);
        a=input.nextInt();
        System.out.println("该正方形的面积="+a*a+"\n");
    }
}
class ProtoTypeManager
{
    private HashMap<String, Shape>ht=new HashMap<String,Shape>();
    public ProtoTypeManager()
    {
        ht.put("Circle",new Circle());
        ht.put("Square",new Square());
    }
    public void addshape(String key,Shape obj)
    {
        ht.put(key,obj);
    }
    public Shape getShape(String key)
    {
        Shape temp=ht.get(key);
        return (Shape) temp.clone();
    }
}

 class ProtoTypeShape{
     public static void main(String[] args){
         ProtoTypeManager protoTypeManager=new ProtoTypeManager();
         Shape circle = (Circle)protoTypeManager.getShape("Circle");
         circle.countArea();
         Shape obj2=(Square)protoTypeManager.getShape("Square");
         obj2.countArea();
     }
}